package com.lottemall.item.web;

import com.lottemall.common.dto.CartDTO;
import com.lottemall.common.vo.PageResult;
import com.lottemall.item.pojo.Sku;
import com.lottemall.item.pojo.Spu;
import com.lottemall.item.pojo.SpuDetail;
import com.lottemall.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * GET   http://api.lottemall.com/api/item/spu/page?key=1&saleable=true&page=1&rows=5 （查询分页商品信息的请求）
 * Post  http://api.lottemall.com/api/item/goods （新增商品的请求）
 * Get   http//api.lottemall.com/api/item/spu/detail/200 (点击修改商品时根据抽象商品SpuId查询商品详情的请求)
 * Get   http://api.lottemall.com/api/item/sku/list?id=200 (点击修改商品时根据抽象商品SpuId查询具体商品集合)
 * PUT   http://api.lottemall.com/api/item/goods (修改商品的保存请求)
 */
@RestController
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询Spu抽象商品
     * @param page
     * @param rows
     * @param saleable // 是否上架，0下架，1上架(T　F)
     * @param key
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<Spu>> querySpuByPsge(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "key", required = false) String key) {
        return ResponseEntity.ok(goodsService.querySpuByPsge(page,rows,saleable,key));
    }

    /**
     * 新增商品
     * info: 前台传送是一个符合json字符串
     *       抽象商品spu、抽象商品的详细属性spu_deatil、具体商品sku、
     * @param spu 具体商品
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody Spu spu){
        goodsService.saveGoods(spu);
        //若上面未出错这里不需要给前台返回什么这里直接返回成功标志
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 根据商品SpuId查询商品SpuDetail详情
     * @param spuId
     * @return
     */
    @GetMapping("/spu/detail/{id}")
    public ResponseEntity<SpuDetail> querySpuDetailById(@PathVariable("id") Long spuId){
        return ResponseEntity.ok(goodsService.querySpuDetailById(spuId));
    }

    /**
     * 根据spu查询下面所有的sku
     * @param spuId
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long spuId){
        return ResponseEntity.ok(goodsService.querySkuBySpuId(spuId));
    }

    /**
     * 根据spu的id集合查询所有sku （暂时用于前台购物车根据浏览器存储的skuids来检查商品是否改变）
     * @Get http://api.lottemall.com/api/item/sku/list/ids?ids=3924115,27359021790,27359021788
     * @param ids
     * @return
     */
    @GetMapping("/sku/list/ids")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(goodsService.querySkuByIds(ids));
    }

    /**
     *  修改商品
     * @param spu
     * @return
     */
    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods(@RequestBody Spu spu){
        goodsService.updateGoods(spu);
        //若上面未出错这里不需要给前台返回什么这里直接返回成功标志
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据id查询spu
     * @param id
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(goodsService.querySpuById(id));
    }

    /**
     * 减库存
     * @param cartDTOS
     * @return
     */
    @PostMapping("stock/decrease") //Post请求是专门传json的
    public ResponseEntity<Void> decreaseStock(@RequestBody List<CartDTO> cartDTOS) {
        goodsService.decreaseStock(cartDTOS);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
